﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThinhTien.Models;
using ThinhTien.EF4;

namespace ThinhTien.Data
{
    public class HomeRepository
    {
        public HomeViewModel GetDataForHomepage()
        {
            var result = new HomeViewModel();

            using (var db = new THINHTIENDBEntities())
            {
                var query = (from category in db.Categories
                             join image in db.Images on category.ImageID equals image.ID into p
                             from image in p.DefaultIfEmpty()
                             where category.IsDeleted == false
                             select new
                             {
                                 category.ID,
                                 category.Name,
                                 category.Description,
                                 category.ParentID,
                                 category.Position,
                                 image.Image1
                             }).ToList().OrderBy(a=>a.Position);

                var parents = query.Where(p => !p.ParentID.HasValue).ToList();
                var childs = query.Where(p => p.ParentID.HasValue).ToList();

                foreach (var parent in parents)
                {
                    var submenus = new List<MenuViewModel>();
                    foreach (var child in childs)
                    {
                        if (parent.ID == child.ParentID)
                        {
                            submenus.Add(new MenuViewModel()
                            {
                                ID = child.ID,
                                Name = child.Name,
                                Description = child.Description
                            });
                        }
                    }

                    result.Menus.Add(new MenuViewModel()
                    {
                        ID = parent.ID,
                        Name = parent.Name,
                        Description = parent.Description,
                        BackgroudImage = parent.Image1,
                        SubMenus = submenus
                    });
                }

                // Get Slide.

                var sl = (from slide in db.Slides
                         join image in db.Images on slide.ImageID equals image.ID into p
                             from image in p.DefaultIfEmpty()
                             select new
                             {
                                 slide.ID,
                                 slide.Title,
                                 slide.Link,
                                 image.Image1
                             }).ToList();

                foreach (var item in sl)
                {
                    result.Slides.Add(new SlideViewModel()
                    {
                        ID = item.ID,
                        Title = item.Title,
                        Link = item.Link,
                        Photo = item.Image1
                    });
                }

                // Get All Product


                var categoryProduct =  (from item in db.Categories
                                        where item.ParentID != null && item.IsDeleted == false
                                        orderby item.Position ascending
                                        select item).Take(3).ToList();

                int i = 0;

                foreach (var cate in categoryProduct)
                {
                    
                    List<ProductViewModel> products = new List<ProductViewModel>();

                    var product = (from pro in db.Products
                                   join image in db.Images on pro.ImageID equals image.ID into p
                                   from image in p.DefaultIfEmpty()
                                   where pro.CategoryID == cate.ID
                                   select new
                                   {
                                       pro.ID,
                                       pro.Name,
                                       pro.Price,
                                       pro.Description,
                                       image.Image1
                                   }).ToList();

                    foreach (var item in product)
                    {
                        products.Add(new ProductViewModel()
                        {
                            ProductID = item.ID,
                            Name = item.Name,
                            Price = item.Price,
                            Description = item.Description,
                            Photo = item.Image1,
                            ProductName = cate.Name
                            
                        });
                    }

                    result.DicProducts.Add(i.ToString(),products);
                    i++;
                }

            }

            return result;
        }






    }
}
